package cn.iocoder.yudao.module.hs.controller.admin.trayscanrecord;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.hs.controller.admin.trayscanrecord.vo.TrayScanRecordPageReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.trayscanrecord.vo.TrayScanRecordRespVO;
import cn.iocoder.yudao.module.hs.controller.admin.trayscanrecord.vo.TrayScanRecordSaveReqVO;
import cn.iocoder.yudao.module.hs.dal.dataobject.trayscanrecord.TrayScanRecordDO;
import cn.iocoder.yudao.module.hs.service.trayscanrecord.TrayScanRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 单号托盘点亮状态记录")
@RestController
@RequestMapping("/hs/tray-scan-record")
@Validated
public class TrayScanRecordController {

    @Resource
    private TrayScanRecordService trayScanRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建单号托盘点亮状态记录")
    @PreAuthorize("@ss.hasPermission('hs:tray-scan-record:create')")
    public CommonResult<Long> createTrayScanRecord(@Valid @RequestBody TrayScanRecordSaveReqVO createReqVO) {
        return success(trayScanRecordService.createTrayScanRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新单号托盘点亮状态记录")
    @PreAuthorize("@ss.hasPermission('hs:tray-scan-record:update')")
    public CommonResult<Boolean> updateTrayScanRecord(@Valid @RequestBody TrayScanRecordSaveReqVO updateReqVO) {
        trayScanRecordService.updateTrayScanRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除单号托盘点亮状态记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hs:tray-scan-record:delete')")
    public CommonResult<Boolean> deleteTrayScanRecord(@RequestParam("id") Long id) {
        trayScanRecordService.deleteTrayScanRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得单号托盘点亮状态记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hs:tray-scan-record:query')")
    public CommonResult<TrayScanRecordRespVO> getTrayScanRecord(@RequestParam("id") Long id) {
        TrayScanRecordDO trayScanRecord = trayScanRecordService.getTrayScanRecord(id);
        return success(BeanUtils.toBean(trayScanRecord, TrayScanRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得单号托盘点亮状态记录分页")
    @PreAuthorize("@ss.hasPermission('hs:tray-scan-record:query')")
    public CommonResult<PageResult<TrayScanRecordRespVO>> getTrayScanRecordPage(@Valid TrayScanRecordPageReqVO pageReqVO) {
        PageResult<TrayScanRecordDO> pageResult = trayScanRecordService.getTrayScanRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TrayScanRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出单号托盘点亮状态记录 Excel")
    @PreAuthorize("@ss.hasPermission('hs:tray-scan-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTrayScanRecordExcel(@Valid TrayScanRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TrayScanRecordDO> list = trayScanRecordService.getTrayScanRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "单号托盘点亮状态记录.xls", "数据", TrayScanRecordRespVO.class,
                        BeanUtils.toBean(list, TrayScanRecordRespVO.class));
    }

}